package com.ruoyi.project.module.pop.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.pop.domain.Pop;
import com.ruoyi.project.module.pop.service.IPopService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * Pop点 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-07-28
 */
@Controller
@RequestMapping("/module/pop")
public class PopController extends BaseController
{
    private String prefix = "module/pop";
	
	@Autowired
	private IPopService popService;
	
	@RequiresPermissions("module:pop:view")
	@GetMapping()
	public String pop()
	{
	    return prefix + "/pop";
	}
	
	
	@GetMapping("/popOptions")
	public String popOptions()
	{
	    return prefix + "/popOptions";
	}
	
	
	/**
	 * 查询Pop点列表
	 */
	@RequiresPermissions("module:pop:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Pop pop)
	{
		startPage();
		if(null != pop.getCpeId()) {
			ServletUtils.getSession().setAttribute("cpeId", pop.getCpeId());
		}
        List<Pop> list = popService.selectPopList(pop);
		return getDataTable(list);
	}
	
	/**
	 * 新增Pop点
	 */
	@RequiresPermissions("module:pop:add")
	@Log(title = "Pop点", action = BusinessType.INSERT)
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改Pop点
	 */
	@RequiresPermissions("module:pop:edit")
	@Log(title = "Pop点", action = BusinessType.UPDATE)
	@GetMapping("/edit/{popId}")
	public String edit(@PathVariable("popId") Integer popId, Model model)
	{
		Pop pop = popService.selectPopById(popId);
		model.addAttribute("pop", pop);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存Pop点
	 */
	@RequiresPermissions("module:pop:save")
	@Log(title = "Pop点", action = BusinessType.SAVE)
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Pop pop)
	{
		if (popService.savePop(pop) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除Pop点
	 */
	@RequiresPermissions("module:pop:remove")
	@Log(title = "Pop点", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = popService.deletePopByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
	
	/**
	 * 删除Pop点
	 */
	@Log(title = "配置Pop点", action = BusinessType.UPDATE)
	@PostMapping( "/editPops")
	@ResponseBody
	public AjaxResult editPops(String ids)
	{	
		int cpeId = (int) ServletUtils.getSession().getAttribute("cpeId");
		int rows = popService. editPopByIds(ids,cpeId);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
}
